# views 路由 + 视图函数
import random

from flask import Blueprint, request, render_template
from .models import *

# 蓝图 需要到__init__.py当中绑定blue
blue = Blueprint('user', __name__)


@blue.route('/')
def index():
    return 'index'


# 一对多, 增加数据班级
@blue.route('/addgrade/')
def add_grade():
    # 添加班级
    grades = []
    for i in range(10):
        grade = Grade()
        grade.name = f'Jay{i}- + {str(random.randint(10, 99))}'
        grades.append(grade)
    try:
        db.session.add_all(grades)
        db.session.commit()
    except Exception as e:
        print('e', e)
        db.session.rollback()
        db.session.flush()

    return 'ok'


# 一对多, 增加数据学生
@blue.route('/addstu/')
def add_stu():
    # 添加学生
    students = []
    for i in range(10, 20):
        stu = Student()
        stu.name = f'Lucy{i}'
        stu.age = i
        # 外键
        stu.grade_id = random.randint(51, 55)
        students.append(stu)
    try:
        db.session.add_all(students)
        db.session.commit()
    except Exception as e:
        print('e', e)
        db.session.rollback()
        db.session.flush()

    return 'ok'


# 修改数据
@blue.route('/updatestu/')
def update_stu():
    # 修改数据
    stu = Student.query.first()
    stu.age = 100
    db.session.commit()

    return 'ok'


# 删除学生
@blue.route('/delstu/')
def del_stu():
    # 删除学生数据
    stu = Student.query.first()
    db.session.delete(stu)
    db.session.commit()

    return 'ok'


# 删除班级
@blue.route('/delgrade/')
def del_grade():
    # 删除班级
    grade = Grade.query.first()
    db.session.delete(grade)
    db.session.commit()

    return 'ok'


# 查询
@blue.route('/getstu/')
def get_stu():
    # 查询学生班级信息
    stu = Student.query.get(16)
    print(stu.name, stu.age)
    # 反向引用
    print(stu.grade_id, stu.grade, stu.grade.name, stu.grade.id)

    grade = Grade.query.get(55)
    print(grade.name)
    print(grade.students)
    for stu in grade.students:
        print(stu.name, stu.age)

    return 'ok'


# 多对多
# 添加用户
@blue.route('/adduser/')
def add_user():
    users = []
    for i in range(10, 14):
        user = UserModel()
        user.name = f'Lucy-{i}'
        user.age = i
        users.append(user)
    try:
        db.session.add_all(users)
        db.session.commit()
    except Exception as e:
        print('e', e)
        db.session.rollback()
        db.session.flush()

    return 'ok'


# 添加电影
@blue.route('/addmovie/')
def add_movie():
    movies = []
    for i in range(10, 14):
        movie = Movie()
        movie.name = f'阿凡达-{i}'
        movies.append(movie)
    try:
        db.session.add_all(movies)
        db.session.commit()
    except Exception as e:
        print('e', e)
        db.session.rollback()
        db.session.flush()

    return 'ok'


@blue.route('/addcollect/')
def add_collect():
    # 用户收藏电影
    user = UserModel.query.get(1)
    movie = Movie.query.get(1)

    # 关联
    user.movies.append(movie)
    db.session.commit()

    return 'ok'


# 查询
@blue.route('/getcollect/')
def get_collect():
    # 查询用户1收藏的电影
    user = UserModel.query.get(1)
    print(user.name, user.age)
    print(user.movies)
    for movie in user.movies:
        print(movie.name)

    # 查询电影4被哪些用户收藏
    movie = Movie.query.get(4)
    print(movie.users)
    print(movie.name)

    return 'ok'


# 修改：和单表操作一样
# 删除：
@blue.route('/deluser/')
def del_user():
    # 级联删除
    user = UserModel.query.get(1)
    db.session.delete(user)
    db.session.commit()

    return 'ok'
